package cn.itcast.controller.company;

import cn.itcast.base.BaseController;
import cn.itcast.domain.Company;
import cn.itcast.service.company.CompanyService;
import cn.itcast.utils.UtilFuns;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.UUID;

@Controller
@RequestMapping(value = "/company")
public class CompanyController extends BaseController {

    @Autowired
    private CompanyService companyService;

   /* @RequestMapping(name = "自己编写的分页查询企业",value = "/list")
    //RequestParam:设置默认值 属性：defaultValue
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize){
       // 分页查询
        PageBean pb=companyService.findPage(pageNum,pageSize);
        request.setAttribute("pb",pb);
        return "company/company-list";
    }*/


    @RequestMapping(name = "pageHelper插件分页查询企业",value = "/list")
    //RequestParam:设置默认值 属性：defaultValue
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize){
        // 分页查询
        PageInfo pb=companyService.findPageTopageHelper(pageNum,pageSize);
        request.setAttribute("pb",pb);
        return "company/company-list";
    }

    @RequestMapping(name = "到新建页面",value = "/toAdd")
    public String toAdd(){

        return "company/company-add";
    }


    @RequestMapping(name = "新增/修改",value = "/edit")
    public String edit(Company company){

        if(UtilFuns.isEmpty(company.getId())){
            //新增
            String id = UUID.randomUUID().toString();
            company.setId(id);
            companyService.save(company);
        }else {
            //修改
            companyService.update(company);
        }
        // 查看最新数据
        return "redirect:/company/list.do";
    }


    @RequestMapping(name = "到修改页面展示要修改的企业的数据",value = "/toUpdate")
    public String toUpdate(String id){

        Company company=companyService.findById(id);
        request.setAttribute("company",company);
        // 查看最新数据
        return "company/company-update";
    }


    @RequestMapping(name = "删除企业",value = "/delete")
    public String delete(String id){

        companyService.del(id);
        // 查看最新数据
        return "redirect:/company/list.do";
    }




}
